package com.yuncheng.spcyApi.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 复制文件-工具类
 */
public class FileCopyUtils {
    private static final Logger log = LoggerFactory.getLogger(FileCopyUtils.class);

    /**
     * 复制文件
     * @param src
     * @param dest
     * @return
     */
    public static boolean copyFile(String src,String dest){
        boolean success = false;
        try{
            FileInputStream fin=new FileInputStream(src);
            BufferedInputStream bin=new BufferedInputStream(fin);
            FileOutputStream fou=new FileOutputStream(dest);
            BufferedOutputStream bou=new BufferedOutputStream(fou);
            byte []buff=new byte[1024*10];
            int len;
            while((len=bin.read(buff))!=-1){
                bou.write(buff, 0, len);
            }
            bou.flush();
            bou.close();
            bin.close();
            fou.close();
            fin.close();
            success = true;
        }catch(IOException e){
            log.error("文件复制出错[IOException]: {}",e.getMessage());
        }
        return success;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        long starttime = System.currentTimeMillis();
        System.out.println("runnin...");
        String src = "D://test/one.pdf";
        System.out.println((new File(src)).length()+"B");
        String dest = "D://test/one"+starttime+".pdf";
        boolean success = copyFile(src,dest);
        if(success){
            System.out.println("文件复制成功。");
        }
        System.out.println("文件复制结束。");
    }

}
